require 'rubygems'
require 'spork'

Spork.prefork do
  # Loading more in this block will cause your tests to run faster. However, 
  # if you change any configuration or code from libraries loaded here, you'll
  # need to restart spork for it take effect.
  # This file is copied to spec/ when you run 'rails generate rspec:install'

# it is done in spork
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)

  require 'rspec/rails'
  require 'capybara/rspec'
  require 'devise/test_helpers'
  require File.join(File.dirname(__FILE__), "my_rest_test_helpers.rb")
  require File.join(File.dirname(__FILE__), "data_definition_helpers.rb")

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|
    # == Mock Framework
    #
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
    #
    # config.mock_with :mocha
    # config.mock_with :flexmock
    # config.mock_with :rr
    config.mock_with :rspec

    # If you're not using ActiveRecord, or you'd prefer not to run each of your
    # examples within a transaction, remove the following line or assign false
    # instead of true.
    config.use_transactional_fixtures = true

    config.include Devise::TestHelpers, :type => :controller
    config.include MyRestTestHelpers::ReturnJsonMather, :type => :controller

  end

  ActiveSupport::Dependencies.clear
  ActiveRecord::Base.instantiate_observers
end

Spork.each_run do
  puts "~~~> Spork.each_run"
  # This code will be run each time you run your specs.
  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
  KfinanceRails2::Application.reload_routes!
  DatabaseCleaner.strategy = :truncation
  DatabaseCleaner.clean

  GC.disable
end

def andRequestParams hash, prefix = nil
  puts "      And: request params - " unless prefix
  hash.each do |k,v|
    label = prefix ? "#{prefix}[#{k}]" : k.to_s

    if v.kind_of? Hash
      andRequestParams(v, label)
    else
      printf("          %s : %s\n", label, v)
    end
  end
end

def responseShouldBe(response, obj, excluding = nil)
  ss = obj.kind_of?(String) ? obj : obj.to_json
  puts " Response: #{response.body}"
  response.body.should be_json_eql(ss).excluding(excluding)
end
